import java.util.Date;
import java.util.List;
import java.util.ArrayList;
public class Admin extends User {
    private List<User> userList = new ArrayList<>();
    private List<Movie>movies=new ArrayList<>();
    private List<Screening>screenings=new ArrayList<>();
    private Log log;
    public void addUser(User user) {
        this. userList.add(user);
    }
    public User getUser(String userId) {//查找用户
        for (User user : userList) {
            if (user.getUserId().equals(userId)) {
                return user;
            }
        }
        return null;
    }
    public User queryUserByUsername(String username) {//查询用户
        System.out.println("=== 调试：查询用户 [" + username + "] ===");
        for (User user : userList) {
            System.out.println("遍历用户：" + user.getUsername());
            if (user.getUsername().equals(username)) {
                System.out.println("找到用户：" + user.getUsername());
                return user;
            }
        }
        System.out.println("=== 调试：未找到用户 ===");
        return null;
    }
    public Admin(String userId, String username, String password, Date registerTime, String phone ) {//管理员构造
        super(userId, username, password, registerTime, UserType.ADMIN, phone);
        this.log = new Log("LOG_ADMIN_" + userId, new Date(), "ADMIN_OPERATE", username);
    }
    public void addAdmin(Admin admin) {//添加管理员
        userList.add(admin);
        log.recordLog("Added admin: " + admin.getUsername());
    }
    public boolean removeUser(User user){//从userList移除用户，实现用户删除管理，返回移除是否成功
        return userList.remove(user);
    }
    public User queryUser(String username) {//查询用户名
        for (User user : userList) {
            if (user.getUsername().equals(username)) {
                return user;
            }
        }
        return null;
    }
    public User queryUserByPhone(String phone){//按手机号查询用户
        for(User user:userList){
            if(user.getPhone().equals(phone)){
                return user;
            }
        }
        return null;
    }
    public void modifyAdminPassword(Admin admin, String newPassword) {//修改管理员密码
        admin.setPassword(newPassword);
        log.recordLog("Modified password for admin: " + admin.getUsername());
    }
    public void resetUserPassword(User user, String newPassword) {//重置普通用户密码
        user.setPassword(newPassword);
        log.recordLog("Reset password for user: " + user.getUsername());
    }
    public void addScreening(Screening screening){//添加场次
        screenings.add(screening);
    }
    public List<User> listAllUsers() {//获取全部用户列表
        return userList;
    }
    public List<Movie>searchMovies(String keyword){
        return new ArrayList<Movie>();
    }
    public void deleteUser(User user) {//删除用户
        if (!user.getUserId().equals("admin")) {
            userList.remove(user);
            log.recordLog("Deleted user: " + user.getUsername());
        }
    }
    public void modifyUserInfo(User user, UserType newType, String newPhone) {//修改用户信息
        if (user.getUserType() == UserType.MANAGER || user.getUserType() == UserType.FRONT_DESK) {
            user.setUserType ( newType);
            user.setPhone (newPhone);
            log.recordLog("Modified info for user: " + user.getUsername());
        }
    }
    public void listAllMovies() {//列出影片信息
        if (movies.isEmpty()) {
            System.out.println("暂无电影信息。");
            return;
        }
        System.out.println("\n=== 所有影片信息 ===");
        for (Movie movie : movies) {
            System.out.println("电影ID: " + movie.getMovieId());
            System.out.println("  标题: " + movie.getTitle());
            System.out.println("  导演: " + movie.getDirector());
            System.out.println("  演员: " + movie.getActors());
            System.out.println("------------------------");
        }
    }
    public Movie findMovieById(String movield) {//按电影Id查找电影
        for (Movie movie : movies) {
            if (movie.getMovieId().equals(movield)) {
                return movie;
            }
        }
        return null;
    }
    public List<Screening>getScreenings(){
        return screenings;
    }
    public void addMovie(Movie movie){//添加电影
        movies.add(movie);
    }
    @Override
    public void login() {
        System.out.println("Admin " + getUsername() + " logged in.");
    }
    @Override
    public void logout() {
        System.out.println("Admin " + getUsername() + " logged out.");
    }
    @Override
    public void managePassword() {
        System.out.println("Admin " + getUsername() + " is managing password.");
    }
}
